#!/bin/bash
check_servicetimeout() {
    echo ""
    echo ""
    echo "############################ 检查service 超时情况 #############################"

    get_ulimit_from_node

    kubectl get svc --all-namespaces  | grep LoadBalancer > /tmp/check_clbtimeout.file
    echo "##############cluster service##############"
    while read line
    do
    ns=`echo "$line" | awk '{print $1}'`
    name=`echo "$line" | awk '{print $2}'`
    echo "######service######"
    get_clbtimeout_from_svc_cluster $ns $name
    done < /tmp/check_clbtimeout.file


    echo ""
    echo "##############local service##############"
    while read line
    do
    ns=`echo "$line" | awk '{print $1}'`
    name=`echo "$line" | awk '{print $2}'`
    echo "######service######"
    get_clbtimeout_from_svc_Local $ns $name
    done < /tmp/check_clbtimeout.file

    echo "######service######"

    return 0

}

check_ingresstimeout() {
    echo ""
    echo ""
    echo "############################ 检查ingress 超时情况 #############################"

    get_ulimit_from_node


    echo "##############ingress##############"
    kubectl get ingress --all-namespaces | grep -v NAMESPACE > /tmp/check_clbtimeout.file
    while read line
    do
    ns=`echo "$line" | awk '{print $1}'`
    name=`echo "$line" | awk '{print $2}'`
    echo "######ingress######"
    get_clbtimeout_from_ingress $ns $name
    done < /tmp/check_clbtimeout.file

    echo "##############ingress##############"

    return 0
}


get_ulimit_from_node() {
    echo ""
    echo "##############节点 ulimit -n情况##############"
    ip=`ifconfig -a | grep -w eth1 -A 7 | grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`
    res=`ulimit -n`
    echo "节点"$ip":"$res
}

get_clbtimeout_from_svc_cluster () {
  ns=$1
  [ -z "$ns" ] && return 1
  name=$2
  [ -z "$name" ] && return 1

  labelk8sapp=""
  targetport=""
  nodeport=""
  event=""
  route_local=""
  ulimit=""


  res=`kubectl get svc $name -n $ns -o yaml | grep "externalTrafficPolicy: Cluster"`
  res=$?
  if [ $res -eq 0 ];then
  echo "service 名字/命名空间:"$name/$ns
  result=`kubectl get svc $name -n $ns -o jsonpath='{.items[*]}{.spec.selector.app}'`
  if [[ ${result[0]} != "" ]];then
  labelk8sapp=${result[0]}
  echo "对应k8s-app label:"$labelk8sapp
  fi

  ip=`ifconfig -a | grep -w eth1 -A 7 | grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`

  event=`kubectl describe svc $name -n $ns | grep Warning | awk '{print $2}'`
  if [[ $event != "" ]];then
  echo "service状态:警告！service有告警:"$event
  else
  echo "service状态:正常"
  fi

  direct=`kubectl get service $name -n $ns -o yaml | grep "service.cloud.tencent.com/direct-access: \"true\""`
  if [[ $direct != "" ]];then
  echo "service pod直连:是"
  else
  echo "service pod直连:否"
  fi

  clbid=`kubectl get service $name -n $ns -o yaml | grep "service.kubernetes.io/loadbalance-id" | awk '{print $2}'`
  if [[ $clbid != "" ]];then
  echo "service clb:"$clbid
  else
  echo "service clb:"
  fi

  clbip=`kubectl get service $name -n $ns -o jsonpath='{.items[*]}{.status.loadBalancer.ingress[0].ip}'`
  if [[ $clbip != "" ]];then
  echo "service clbip:"$clbip
  else
  echo "service clbip:"
  fi

  serviceport=`kubectl get svc $name -n $ns -o jsonpath='{.items[*]}{.spec.ports[0].port}'`
  echo "service 服务端口:"$serviceport

  serviceEp=`kubectl get ep $name -n $ns -o jsonpath='{range .subsets[0].addresses[*].ip}{},'`
  echo "servcie endpoint:"$serviceEp

  nodeport=`kubectl get svc $name -n $ns -o jsonpath='{.items[*]}{.spec.ports[0].nodePort}'`
  echo "nodePort端口:"$nodeport
  targetport=`kubectl get svc $name -n $ns -o jsonpath='{.items[*]}{.spec.ports[0].targetPort}'`
  echo "后端pod监听端口:"$targetport

  echo "后端pod:"
  podlist=`kubectl get po -n $ns -o jsonpath='{range .items[?(@.metadata.labels.app=="'$labelk8sapp'")]}{.metadata.name}{"\n"}{end}'`
  for element in ${podlist[*]}
  do
   podstatus=`kubectl get pod $element -n $ns | grep $element | awk '{print $3}'`
   podip=`kubectl get pod $element -n $ns -o wide | grep $element | awk '{print $6}'`
   echo "pod 名字:"$element
   echo "pod 状态:"$podstatus
   echo "pod ip:"$podip
   if [[ $podstatus == "Running" ]];then
    route_local=`kubectl exec -i $element -n $ns -- /bin/sh -c 'cat /proc/sys/net/ipv4/conf/all/route_localnet'`
    echo "pod内核参数net.ipv4.conf.all.route_localnet:"$route_local
    ulimit=`kubectl exec -i $element -n $ns -- /bin/sh -c 'ulimit -n'`
    echo "pod内核参数ulimit:"$ulimit
   else
    echo "pod内核参数net.ipv4.conf.all.route_localnet:警告！pod状态异常"
    echo "pod内核参数ulimit:警告！pod状态异常"
   fi
  done
  fi

}


get_clbtimeout_from_svc_Local () {
  ns=$1
  [ -z "$ns" ] && return 1
  name=$2
  [ -z "$name" ] && return 1

  labelk8sapp=""
  targetport=""
  nodeport=""
  event=""
  route_local=""
  ulimit=""


  res=`kubectl get svc $name -n $ns -o yaml | grep "externalTrafficPolicy: Local"`
  res=$?
  if [ $res -eq 0 ];then
  echo "service 名字/命名空间:"$name/$ns
  result=`kubectl get svc $name -n $ns -o jsonpath='{.items[*]}{.spec.selector.app}'`
  if [[ ${result[0]} != "" ]];then
  labelk8sapp=${result[0]}
  echo "对应k8s-app label:"$labelk8sapp
  fi

  ip=`ifconfig -a | grep -w eth1 -A 7 | grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`

  event=`kubectl describe svc $name -n $ns | grep Warning | awk '{print $2}'`
  if [[ $event != "" ]];then
  echo "service状态:警告！service有告警:"$event
  else
  echo "service状态:正常"
  fi

  direct=`kubectl get service $name -n $ns -o yaml | grep "service.cloud.tencent.com/direct-access: \"true\""`
  if [[ $direct != "" ]];then
  echo "service pod直连:是"
  else
  echo "service pod直连:否"
  fi

  clbid=`kubectl get service $name -n $ns -o yaml | grep "service.kubernetes.io/loadbalance-id" | awk '{print $2}'`
  if [[ $clbid != "" ]];then
  echo "service clb:"$clbid
  else
  echo "service clb:"
  fi

  clbip=`kubectl get service $name -n $ns -o jsonpath='{.items[*]}{.status.loadBalancer.ingress[0].ip}'`
  if [[ $clbip != "" ]];then
  echo "service clbip:"$clbip
  else
  echo "service clbip:"
  fi

  serviceport=`kubectl get svc $name -n $ns -o jsonpath='{.items[*]}{.spec.ports[0].port}'`
  echo "service 服务端口:"$serviceport

  serviceEp=`kubectl get ep $name -n $ns -o jsonpath='{range .subsets[0].addresses[*].ip}{},'`
  echo "servcie endpoint:"$serviceEp

  nodeport=`kubectl get svc $name -n $ns -o jsonpath='{.items[*]}{.spec.ports[0].nodePort}'`
  echo "nodePort端口:"$nodeport
  targetport=`kubectl get svc $name -n $ns -o jsonpath='{.items[*]}{.spec.ports[0].targetPort}'`
  echo "后端pod监听端口:"$targetport

  echo "后端pod:"
  podlist=`kubectl get po -n $ns -o jsonpath='{range .items[?(@.metadata.labels.app=="'$labelk8sapp'")]}{.metadata.name}{"\n"}{end}'`
  for element in ${podlist[*]}
  do
   podstatus=`kubectl get pod $element -n $ns | grep $element | awk '{print $3}'`
   podip=`kubectl get pod $element -n $ns -o wide | grep $element | awk '{print $6}'`
   echo "pod 名字:"$element
   echo "pod 状态:"$podstatus
   echo "pod ip:"$podip
   if [[ $podstatus == "Running" ]];then
    route_local=`kubectl exec -i $element -n $ns -- /bin/sh -c 'cat /proc/sys/net/ipv4/conf/all/route_localnet'`
    echo "pod内核参数net.ipv4.conf.all.route_localnet:"$route_local
    ulimit=`kubectl exec -i $element -n $ns -- /bin/sh -c 'ulimit -n'`
    echo "pod内核参数ulimit:"$ulimit
   else
    echo "pod内核参数net.ipv4.conf.all.route_localnet:警告！pod状态异常"
    echo "pod内核参数ulimit:警告！pod状态异常"
   fi
  done
  fi

}

get_clbtimeout_from_ingress () {
  ns=$1
  [ -z "$ns" ] && return 1
  name=$2
  [ -z "$name" ] && return 1

  servicename=""

  kubectl get ingress $name -n $ns -o yaml | grep "serviceName" | grep -v "kubernetes.io/ingress.http-rules" | grep -v "kubernetes.io/ingress.https-rules" > /tmp/check_clbtimeout_ingress.file
  echo "ingress 名字/命名空间:"$name/$ns

  ip=`ifconfig -a | grep -w eth1 -A 7 | grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`

  event=`kubectl describe ingress $name -n $ns | grep Warning | awk '{print $2}'`
  if [[ $event != "" ]];then
  echo "ingress 状态:警告！ingress有告警:"$event
  else
  echo "ingress状态:正常"
  fi

  direct=`kubectl get ingress $name -n $ns -o yaml | grep "ingress.cloud.tencent.com/direct-access: \"true\""`
  if [[ $direct != "" ]];then
  echo "ingress pod直连:是"
  else
  echo "ingress pod直连:否"
  fi

  clbid=`kubectl get ingress $name -n $ns -o yaml | grep "kubernetes.io/ingress.qcloud-loadbalance-id" | awk '{print $2}'`
  if [[ $clbid != "" ]];then
  echo "ingress clb:"$clbid
  else
  echo "ingress clb:"
  fi

  clbip=`kubectl get ingress $name -n $ns -o jsonpath='{.items[*]}{.status.loadBalancer.ingress[0].ip}'`
  if [[ $clbip != "" ]];then
  echo "ingress clbip:"$clbip
  else
  echo "ingress clbip:"
  fi


  while read line
  do
  servicename=`echo "$line" | awk '{print $2}'`
  get_service_from_ingress $ns $servicename

  done < /tmp/check_clbtimeout_ingress.file


}

get_service_from_ingress() {
    ns=$1
    [ -z "$ns" ] && return 1
    name=$2
    [ -z "$name" ] && return 1

    #res=`kubectl get svc $name -n $ns -o yaml | grep LoadBalancer`
    #if [[ $res == "" ]];then
    get_clbtimeout_from_svc_Local $ns $name
    get_clbtimeout_from_svc_cluster $ns $name
    #fi
}


check_clb() {

    echo "############################ 检查集群使用clb情况 #############################"

    kubectl get svc --all-namespaces  | grep LoadBalancer > /tmp/check_clb.file
    echo "cluster service"
    while read line
    do
    ns=`echo "$line" | awk '{print $1}'`
    name=`echo "$line" | awk '{print $2}'`
    #echo $status
    get_clb_from_svc_cluster $ns $name
    done < /tmp/check_clb.file

    echo ""
    echo "local service"
    while read line
    do
    ns=`echo "$line" | awk '{print $1}'`
    name=`echo "$line" | awk '{print $2}'`
    #echo $status
    get_clb_from_svc_Local $ns $name
    done < /tmp/check_clb.file

    echo ""
    echo "ingress"
    kubectl get ingress --all-namespaces | grep -v NAMESPACE > /tmp/check_clb.file
    while read line
    do
    ns=`echo "$line" | awk '{print $1}'`
    name=`echo "$line" | awk '{print $2}'`
    #echo $status
    get_clb_from_ingress $ns $name
    done < /tmp/check_clb.file

    return 0

}


get_clb_from_svc_cluster () {
  ns=$1
  [ -z "$ns" ] && return 1
  name=$2
  [ -z "$name" ] && return 1

  res=`kubectl get svc $name -n $ns -o yaml | grep "externalTrafficPolicy: Cluster"`
  res=$?
  if [ $res -eq 0 ];then
  result=`kubectl get svc $name -n $ns -o jsonpath='{.items[*]}{.metadata.name} {.metadata.namespace} {.metadata.annotations.service\.kubernetes\.io\/loadbalance-id }{"\n"}'`
  echo $result
  fi

}

get_clb_from_svc_Local () {
  ns=$1
  [ -z "$ns" ] && return 1
  name=$2
  [ -z "$name" ] && return 1

  kubectl get svc $name -n $ns -o yaml | grep "externalTrafficPolicy: Local"
  res=$?
  if [ $res -eq 0 ];then
  result=`kubectl get svc $name -n $ns -o jsonpath='{.items[*]}{.metadata.name} {.metadata.namespace} {.metadata.annotations.service\.kubernetes\.io\/loadbalance-id }{"\n"}'`
  echo $result
  fi

}


get_clb_from_ingress () {
  ns=$1
  [ -z "$ns" ] && return 1
  name=$2
  [ -z "$name" ] && return 1

  result=`kubectl get ingress $name -n $ns -o jsonpath='{.items[*]}{.metadata.name} {.metadata.namespace} {.metadata.annotations.kubernetes\.io\/ingress\.qcloud-loadbalance-id }{"\n"}'`

  #result=`kubectl get ingress $name -n $ns -o yaml | grep kubernetes.io/ingress.qcloud-loadbalance-id`

  #clbid=${result:0-11}

  echo $result

}



check_servicetimeout
check_ingresstimeout
check_clb
